package com.campus.food.controller;

import com.campus.food.model.entity.OrderDetail;
import com.campus.food.service.OrderDetailService;
import com.campus.food.util.Result;
import com.campus.food.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 订单详情控制器
 * 处理订单详情相关的HTTP请求
 * 
 * @author Claude3.7 Sonnet
 * @since 2025-05-29
 */
@RestController
@RequestMapping("/api/order-details")
@Validated
public class OrderDetailController {
    
    @Autowired
    private OrderDetailService orderDetailService;
    
    /**
     * 添加订单详情
     * POST /api/order-details
     */
    @PostMapping
    public Result<OrderDetail> addOrderDetail(@Valid @RequestBody OrderDetail orderDetail) {
        try {
            OrderDetail savedOrderDetail = orderDetailService.addOrderDetail(orderDetail);
            return Result.success("订单详情添加成功", savedOrderDetail);
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("添加订单详情失败，请稍后重试");
        }
    }
    
    /**
     * 根据ID获取订单详情
     * GET /api/order-details/{id}
     */
    @GetMapping("/{id}")
    public Result<OrderDetail> getOrderDetailById(@PathVariable @NotNull Long id) {
        try {
            OrderDetail orderDetail = orderDetailService.getOrderDetailById(id);
            return Result.success(orderDetail);
        } catch (BusinessException e) {
            return Result.notFound(e.getMessage());
        } catch (Exception e) {
            return Result.error("获取订单详情失败");
        }
    }
    
    /**
     * 更新订单详情
     * PUT /api/order-details/{id}
     */
    @PutMapping("/{id}")
    public Result<String> updateOrderDetail(@PathVariable @NotNull Long id,
                                          @RequestBody OrderDetail orderDetail) {
        try {
            orderDetail.setId(id);
            boolean success = orderDetailService.updateOrderDetail(orderDetail);
            if (success) {
                return Result.success("订单详情更新成功");
            } else {
                return Result.error("订单详情更新失败");
            }
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("更新订单详情失败");
        }
    }
    
    /**
     * 删除订单详情
     * DELETE /api/order-details/{id}
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteOrderDetail(@PathVariable @NotNull Long id) {
        try {
            boolean success = orderDetailService.deleteOrderDetail(id);
            if (success) {
                return Result.success("订单详情删除成功");
            } else {
                return Result.error("订单详情删除失败");
            }
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("删除订单详情失败");
        }
    }
    
    /**
     * 根据订单ID获取订单详情列表
     * GET /api/order-details/order/{orderId}
     */
    @GetMapping("/order/{orderId}")
    public Result<List<OrderDetail>> getOrderDetailsByOrderId(@PathVariable @NotNull Long orderId) {
        try {
            List<OrderDetail> orderDetails = orderDetailService.getOrderDetailsByOrderId(orderId);
            return Result.success(orderDetails);
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("获取订单详情列表失败");
        }
    }
    
    /**
     * 根据订单ID删除所有订单详情
     * DELETE /api/order-details/order/{orderId}
     */
    @DeleteMapping("/order/{orderId}")
    public Result<String> deleteOrderDetailsByOrderId(@PathVariable @NotNull Long orderId) {
        try {
            boolean success = orderDetailService.deleteOrderDetailsByOrderId(orderId);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("删除订单详情失败");
        }
    }
    
    /**
     * 批量添加订单详情
     * POST /api/order-details/batch
     */
    @PostMapping("/batch")
    public Result<String> addOrderDetailsBatch(@RequestBody List<OrderDetail> orderDetails) {
        try {
            boolean success = orderDetailService.addOrderDetailsBatch(orderDetails);
            if (success) {
                return Result.success("批量添加成功");
            } else {
                return Result.error("批量添加失败");
            }
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("批量添加订单详情失败");
        }
    }
    
    /**
     * 根据菜品ID获取订单详情列表
     * GET /api/order-details/food/{foodId}
     */
    @GetMapping("/food/{foodId}")
    public Result<List<OrderDetail>> getOrderDetailsByFoodId(@PathVariable @NotNull Long foodId) {
        try {
            List<OrderDetail> orderDetails = orderDetailService.getOrderDetailsByFoodId(foodId);
            return Result.success(orderDetails);
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("获取订单详情列表失败");
        }
    }
    
    /**
     * 获取菜品销量统计
     * GET /api/order-details/food/{foodId}/sales
     */
    @GetMapping("/food/{foodId}/sales")
    public Result<Integer> getSalesCountByFoodId(@PathVariable @NotNull Long foodId) {
        try {
            Integer salesCount = orderDetailService.getSalesCountByFoodId(foodId);
            return Result.success(salesCount);
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("获取销量统计失败");
        }
    }
    
    /**
     * 批量根据订单ID获取订单详情
     * POST /api/order-details/orders/batch
     */
    @PostMapping("/orders/batch")
    public Result<List<OrderDetail>> getOrderDetailsByOrderIds(@RequestBody List<Long> orderIds) {
        try {
            List<OrderDetail> orderDetails = orderDetailService.getOrderDetailsByOrderIds(orderIds);
            return Result.success(orderDetails);
        } catch (Exception e) {
            return Result.error("获取订单详情失败");
        }
    }
    
    /**
     * 计算订单详情总金额
     * GET /api/order-details/order/{orderId}/total
     */
    @GetMapping("/order/{orderId}/total")
    public Result<Double> calculateOrderDetailTotal(@PathVariable @NotNull Long orderId) {
        try {
            Double total = orderDetailService.calculateOrderDetailTotal(orderId);
            return Result.success(total);
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("计算总金额失败");
        }
    }
    
    /**
     * 验证订单详情数据完整性
     * GET /api/order-details/order/{orderId}/validate
     */
    @GetMapping("/order/{orderId}/validate")
    public Result<Boolean> validateOrderDetails(@PathVariable @NotNull Long orderId) {
        try {
            boolean isValid = orderDetailService.validateOrderDetails(orderId);
            return Result.success(isValid);
        } catch (BusinessException e) {
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            return Result.error("验证订单详情失败");
        }
    }
} 